package com.utils.network.api;

import com.utils.network.BuildConfig;
import com.utils.network.conver.GsonConverterFactory;
import com.utils.network.factory.LiveDataCallAdapterFactory;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;

public class Api<T> {
    private static Api sApi;
    private Class<T> mClass;

    public Api(Class<T> pClass) {
        this.mClass = pClass;
    }

    public static Api getInstance(Class pClass) {
        if (null == sApi) sApi = new Api(pClass);
        return sApi;
    }

    public T get() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(ApiConfig.TIME_OUT, TimeUnit.SECONDS);
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            builder.addInterceptor(loggingInterceptor);
        }
        return new Retrofit.Builder().baseUrl(ApiConfig.BASE_URL).client(builder.build())
                .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(mClass);
    }
}
